//
//  JPTanKuangAlert.m
//  Jpod
//
//  Created by zenox on 2022/3/2.
//

#import "JPTanKuangAlert.h"

//颜色宏
#define Color_HEX(rgbValue) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:1]
#define Color_HEXA(rgbValue, a) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:(a)]
#define Color(r, g, b) [UIColor colorWithRed:((r) / 255.0) green:((g) / 255.0) blue:((b) / 255.0) alpha:1.0]
#define ColorAlpha(r, g, b, a) [UIColor colorWithRed:((r) / 255.0) green:((g) / 255.0) blue:((b) / 255.0) alpha:(a)]

static NSString* const TYFBetObbAlertViewKey = @"TYFBetObbAlertViewKey";

@interface JPTanKuangAlert ()
//白色框
@property (nonatomic,strong) UIView *whiewView;
//标题
@property (nonatomic,strong) UILabel *titleLabel;
//子标题
@property (nonatomic,strong) UILabel *subLabel;
//不再提示
@property (nonatomic,strong) UILabel *noTipLabel;
//选择按钮
@property (nonatomic,strong) UILabel *chooseLabel;

@property(nonatomic,strong) UIButton *chooseBtn;
//线条
@property (nonatomic,strong) UIView *lineView;
//我知道了 按钮
@property (nonatomic,strong) UIButton *okBtn;

@property (nonatomic,assign) NSInteger statusTag;

@end

@implementation JPTanKuangAlert


-(instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self loadWithUI];
    }
    return self;
}
-(void)loadWithUI{
    self.backgroundColor =  ColorAlpha(0, 0, 0, 0.5);
    [self addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapViewClick)]];
    [self addSubview:self.whiewView];
    
    [self.whiewView addSubview:self.titleLabel];
    [self.whiewView addSubview:self.subLabel];
    [self.whiewView addSubview:self.noTipLabel];
    [self.whiewView addSubview:self.chooseLabel];
    [self.whiewView addSubview:self.chooseBtn];
    [self.whiewView addSubview:self.lineView];
    [self.whiewView addSubview:self.okBtn];
    
    if ([JPTanKuangAlert getTYFBetObbAlertViewKeyWithStr].integerValue == 1) {
        self.chooseLabel.backgroundColor = Color(89, 126, 247);
        self.chooseBtn.layer.borderColor = Color(89, 126, 247).CGColor;
    } else {
        self.chooseLabel.backgroundColor = UIColor.clearColor;
        self.chooseBtn.layer.borderColor = Color(203, 206, 216).CGColor;
    }
    self.statusTag = [JPTanKuangAlert getTYFBetObbAlertViewKeyWithStr].integerValue;
    
    self.whiewView.userInteractionEnabled = YES;
//    [self.whiewView ty_addTapActionWithBlock:^(UIGestureRecognizer *gestureRecoginzer) {
//        return;
//    }];
    
    self.noTipLabel.userInteractionEnabled = YES;
    __weak typeof(self) weakSelf = self;
//    [self.noTipLabel ty_addTapActionWithBlock:^(UIGestureRecognizer *gestureRecoginzer) {
//        [weakSelf chooseClick];
//    }];
}

#pragma mark -- click fun
+(JPTanKuangAlert *)shareView{
    JPTanKuangAlert *alter = [[JPTanKuangAlert alloc] initWithFrame:CGRectMake(0, 0, UIScreen.mainScreen.bounds.size.width, UIScreen.mainScreen.bounds.size.height)];
    [[[UIApplication sharedApplication] keyWindow] addSubview:alter];
    return alter;
}
-(void)tapViewClick{
    [self removeFromSuperview];
}
-(void)chooseClick{
    if (self.statusTag == 0) {
        self.chooseLabel.backgroundColor = [UIColor whiteColor];
        self.chooseBtn.layer.borderColor = Color(203, 206, 216).CGColor;
        self.statusTag = 1;
    } else {
        self.chooseLabel.backgroundColor = UIColor.clearColor;
        self.chooseBtn.layer.borderColor = (__bridge CGColorRef _Nullable)([UIColor whiteColor]);
        self.statusTag = 0;
    }
}
-(void)okClick{
    if (self.statusTag == 1) {
        [[NSUserDefaults standardUserDefaults] setObject:@"1" forKey:TYFBetObbAlertViewKey];
    } else {
        [[NSUserDefaults standardUserDefaults] setObject:@"0" forKey:TYFBetObbAlertViewKey];
    }
    self.okBlock();
    [self removeFromSuperview];
}
//获取是否不显示
+(NSString *)getTYFBetObbAlertViewKeyWithStr{
    NSString *str = @"0";
    if ([[NSUserDefaults standardUserDefaults] objectForKey:TYFBetObbAlertViewKey]) {
        str = [[NSUserDefaults standardUserDefaults] objectForKey:TYFBetObbAlertViewKey];
    }
    return str;
}

#pragma mark - Layout
-(void)layoutSubviews {
    
    self.whiewView.frame = CGRectMake(0, 0, 270, 185);
    
    self.whiewView.center = self.center;
    
    self.titleLabel.frame = CGRectMake(20, 20, CGRectGetWidth(self.whiewView.frame) - 40, 25);
    
    self.subLabel.frame = CGRectMake(20, CGRectGetMaxY(self.titleLabel.frame) + 12, CGRectGetWidth(self.whiewView.frame) - 40, 20);
    
    self.noTipLabel.frame = CGRectMake((CGRectGetWidth(self.whiewView.frame) - 60)/2, CGRectGetMaxY(self.subLabel.frame) + 12, 60, 20);
    
    self.chooseLabel.frame = CGRectMake(CGRectGetMinX(self.noTipLabel.frame) - 8 - 5 - 3, CGRectGetMinY(self.noTipLabel.frame) + 6, 8, 8);
    self.chooseBtn.frame = CGRectMake(CGRectGetMinX(self.noTipLabel.frame) - 14 - 5, CGRectGetMinY(self.noTipLabel.frame) + 3, 14, 14);
    
    self.lineView.frame = CGRectMake(0, CGRectGetMaxY(self.noTipLabel.frame) + 12 + 0.5, CGRectGetWidth(self.whiewView.frame), 0.5);
    
    self.okBtn.frame = CGRectMake(20, CGRectGetHeight(self.whiewView.frame) - 25 - 13, CGRectGetWidth(self.whiewView.frame) - 40, 25);
    
}

#pragma mark - get set
-(UILabel *)noTipLabel{
    if (!_noTipLabel) {
        _noTipLabel = [UILabel new];
        _noTipLabel.text = @"不再弹出";
//        _noTipLabel.font = PingSCBold(14);
//        _noTipLabel.textColor = [UIColor colorForKey:@"TYFProfessorView_titleColor_0.4"];
        _noTipLabel.textAlignment = NSTextAlignmentLeft;
    }
    return _noTipLabel;
}
-(UILabel *)subLabel{
    if (!_subLabel) {
        _subLabel = [UILabel new];
        _subLabel.text = @"请以实时盘口、赔率为准!";
//        _subLabel.font = PingSCBold(14);
//        _subLabel.textColor = [UIColor colorForKey:@"TYFProfessorView_titleColor_0.7"];
        _subLabel.textAlignment = NSTextAlignmentCenter;
    }
    return _subLabel;
}
-(UILabel *)titleLabel{
    if (!_titleLabel) {
        _titleLabel = [UILabel new];
        _titleLabel.text = @"注意";
//        _titleLabel.font = PingSCBold(18);
//        _titleLabel.textColor = [UIColor colorForKey:@"TYFBetObbAlertView_titleColor"];
        _titleLabel.textAlignment = NSTextAlignmentCenter;
    }
    return _titleLabel;
}
-(UIButton *)okBtn{
    if (!_okBtn) {
        _okBtn = [UIButton new];
        [_okBtn setTitle:@"我知道了" forState:UIControlStateNormal];
//        [_okBtn setTitleColor:[UIColor colorForKey:@"TYFBetObbAlertView_okBtnTextColor"] forState:UIControlStateNormal];
//        _okBtn.titleLabel.font = PingSCRegular(18);
        [_okBtn addTarget:self action:@selector(okClick) forControlEvents:UIControlEventTouchUpInside];
    }
    return _okBtn;
}
-(UILabel *)chooseLabel{
    if (!_chooseLabel) {
        _chooseLabel = [UILabel new];
        _chooseLabel.layer.cornerRadius = 4;
        _chooseLabel.layer.masksToBounds = YES;
    }
    return _chooseLabel;
}
-(UIButton *)chooseBtn{
    if (!_chooseBtn) {
        _chooseBtn = [UIButton new];
        _chooseBtn.backgroundColor = UIColor.clearColor;
        _chooseBtn.layer.borderWidth = 1;
        _chooseBtn.layer.cornerRadius = 7;
        _chooseBtn.layer.masksToBounds = YES;
        [_chooseBtn addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(chooseClick)]];
    }
    return _chooseBtn;
}
-(UIView *)lineView{
    if (!_lineView) {
        _lineView = [UIView new];
//        _lineView.backgroundColor = [UIColor colorForKey:@"TYFBetObbAlertView_lineViewColor"];
    }
    return _lineView;
}
-(UIView *)whiewView{
    if (!_whiewView) {
        _whiewView = [UIView new];
//        _whiewView.backgroundColor = [UIColor colorForKey:@"TYFBetObbAlertView_bgColor"];
        _whiewView.layer.masksToBounds = YES;
        _whiewView.layer.cornerRadius = 10.0f;
    }
    return _whiewView;
}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
